﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.DAL;

namespace MyDMS_ExtJS
{
    public partial class PrintMenu : System.Web.UI.Page
    {
        MyDMSEntity _myDmsEntity = new MyDMSEntity();
        private List<int> _proj_code, _task_code;
        protected List<int> _dept_code;
        protected List<int> _folder_code;
        protected void Page_Load(object sender, EventArgs e)
        {
            #region 处理参数

            if (Request.QueryString["dept"] != null)
            {
                _dept_code = Conver2List(Request.QueryString["dept"].ToString());
            }
            if (Request.QueryString["proj"] != null)
            {
                _proj_code = Conver2List(Request.QueryString["proj"].ToString());
            }
            if (Request.QueryString["folder"] != null)
            {
                _folder_code = Conver2List(Request.QueryString["folder"].ToString());
            }
            if (Request.QueryString["task"] != null)
            {
                _task_code = Conver2List(Request.QueryString["task"].ToString());
            }

            #endregion

            StringBuilder sb = new StringBuilder();

            if (_folder_code != null)
            {
                foreach (var folder in _folder_code)
                {
                    var folderName = _myDmsEntity.Folder.FirstOrDefault(t => t.Folder_Code == folder).Folder_Title;
                    var documents = _myDmsEntity.Document.Where(d => d.Folder_Code == folder);
                    sb.Append(folderName + "\n:");
                    foreach (var document in documents)
                    {
                        //if (document.Folder_Code.HasValue)
                        //{
                        sb.Append("------" + document.Document_Name + "\n");
                        //}
                    }
                }
            }
            if (_task_code != null)
            {
                foreach (var task in _task_code)
                {
                    var taskName = _myDmsEntity.Task.FirstOrDefault(t => t.Task_Code == task).Task_Title;
                    var documents = _myDmsEntity.Document.Where(d => d.Task_Code == task);
                    sb.Append(taskName + "\n:");
                    foreach (var document in documents)
                    {
                        //if (document.Folder_Code.HasValue)
                        //{
                        sb.Append("------" + document.Document_Name + "\n");
                        //}
                    }
                }
            }
            if (_proj_code != null)
            {
                foreach (var proj in _proj_code)
                {
                    var projName = _myDmsEntity.Project.FirstOrDefault(t => t.Project_Code == proj).Project_Title;
                    sb.Append(projName + "\n:");
                    var tasks = _myDmsEntity.Task.Where(t => t.Project_Code == proj);
                    foreach (var task in tasks)
                    {
                        sb.Append("------" + projName + "\n:");
                        var documents = _myDmsEntity.Document.Where(d => d.Task_Code == task.Task_Code);
                        foreach (var document in documents)
                        {
                            //if (document.Folder_Code.HasValue)
                            //{
                            sb.Append("------------" + document.Document_Name + "\n");
                            //}
                        }
                    }
                }
            }
            Response.Write(sb.ToString());
        }

        private List<int> Conver2List(string input)
        {
            List<int> list=new List<int>();
            foreach (var i in
            input.Split(',').ToList())
            {
                if (!string.IsNullOrEmpty(i))
                {
                    list.Add(Convert.ToInt32(i.Trim(',')));
                }
            }
            return list;
        }
    }
}